﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using nkstdgia.common.ThamDinhGia.BusinessLogic;
using nkstdgia.common.ThamDinhGia.BusinessObjects;
using wsRK.AppUser;
using wsRK.Constant;
using wsRK.Web;

namespace nkstdgia.main
{
    //*********************************************************************************
    //IMPORTANT
    //LAY FIELD THUA_DAT.NOI_DUNG_LAN_DUONG LAM TEN NGUOI THU NHAP. 
    //*********************************************************************************
    public partial class ctbangkhaosat : WebPage
    {
        #region Variable
        public string Google_API_key = ConfigurationManager.AppSettings["Google_API_key"];
        private EnumType.ProcessMode _mode = EnumType.ProcessMode.View;
        private string _strUserCreate { get { return ViewState["_strUserCreate"] != null ? (string)ViewState["_strUserCreate"] : ""; } set { ViewState["_strUserCreate"] = value; } }
        #endregion

        #region Construction
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.processThoiDiemGiaoDich();
                this._getDataQuan();
                this.ddlQuan.SelectedIndex = 0;
                this._getDataPhuong();
                CalendarExtender2.SelectedDate = DateTime.Now;
            }

            if (Request.QueryString["phieuID"] == null)
            {
                this.btnAdd.Visible = true;
                this.btnSaveTemp.Visible = true;
                this.btnSave.Visible = false;

                this.btnDelete.Visible = false;
                this.btnDelete1.Visible = false;

                this.btnAdd1.Visible = true;
                this.btnSaveTemp1.Visible = true;
                this.btnSave1.Visible = false;

                this.btnExport.Visible = false;

                this._mode = EnumType.ProcessMode.Add;

                /******* check right*******/
                if (!User.IsInRole("administrator") && !User.IsInRole("Quản trị viên") && !User.IsInRole("Sinh viên"))
                {
                    Response.Redirect("~/default.aspx");
                }
                /**************************/
            }
            else
            {
                this.btnAdd.Visible = false;
                this.btnSaveTemp.Visible = false;
                this.btnSave.Visible = true;

                this.btnDelete.Visible = true;
                this.btnDelete1.Visible = true;

                this.btnAdd1.Visible = false;
                this.btnSaveTemp1.Visible = false;
                this.btnSave1.Visible = true;

                this._mode = EnumType.ProcessMode.Edit;
                try
                {
                    long maPKS = Int32.Parse(Request.QueryString["phieuID"]);
                    string activeflag = "";
                    if (!IsPostBack)
                    {
                        activeflag = this.loadSavedRecord(maPKS);
                        this.maPhieuKhaoSat.Value = maPKS.ToString();
                    }

                    #region check permission
                    MembershipUser loginUser = MembershipDataservice.getUser(User.Identity.Name);
                    if (User.IsInRole("administrator") || (loginUser != null && this._strUserCreate == loginUser.ProviderUserKey.ToString()))
                    {
                        if (activeflag == "T")
                        {
                            this.btnSaveTemp.Visible = true;
                            this.btnSaveTemp1.Visible = true;
                            this.btnExport.Visible = false;
                        }
                        else
                        {
                            this.btnExport.Visible = true;
                            this.btnExport.OnClientClick =
                                "javascript:window.location.href='/FileDownload.ashx?type=exportptd&id=" + maPKS + "';return false;";
                        }
                    }
                    else
                    {
                        this.btnAdd.Visible = false;
                        this.btnDelete.Visible = false;
                        this.btnDelete1.Visible = false;
                        this.btnExport.Visible = false;
                        this.btnSave.Visible = false;
                        this.btnSave1.Visible = false;
                        this._disableAllControl(this.Controls);
                    }
                    #endregion

                    if (Request.QueryString["msg"] != null)
                        base.showMasterMessage(Request.QueryString["msg"]);
                }
                catch (Exception)
                {
                    base.showMasterMessage("ID phiếu khảo sát cần tải không chính xác !");
                }
            }
        }
        #endregion

        #region Get Data
        private void processThoiDiemGiaoDich()
        {
            for (int i = 1; i <= 12; i++)
            {
                this.ddlTDGDThang.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = DateTime.Now.Year; i >= 1900; i--)
            {
                this.ddlTDGDNam.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }

        private void _getDataQuan()
        {
            List<THAM_SO> lsThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
            lsThamSo = (from t in lsThamSo
                        where t.LOAI_THAM_SO == "LOAI_QUAN"
                        orderby t.SEQUENCE ascending
                        select t).ToList();
            this.ddlQuan.DataSource = lsThamSo;
            this.ddlQuan.DataBind();

            if (lsThamSo.Count > 0)
            {
                this.ddlQuan.SelectedIndex = 0;
            }
        }

        private void _getDataPhuong()
        {
            int intMaThamSoQuan = int.Parse(this.ddlQuan.SelectedValue);
            this._getDataPhuong(intMaThamSoQuan);
        }

        private void _getDataPhuong(int intMaThamSoQuan)
        {
            List<THAM_SO> lsThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
            lsThamSo = (from t in lsThamSo
                        where t.LOAI_THAM_SO == "LOAI_PHUONG" && t.PARENT_ID == intMaThamSoQuan
                        orderby t.SEQUENCE ascending
                        select t).ToList();
            this.ddlPhuong.DataSource = lsThamSo;
            this.ddlPhuong.DataBind();
        }
        #endregion

        #region Event
        protected void ddlQuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._getDataPhuong();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //this.addNewRecord();     
            try
            {
                long maHS = this.addNewRecord("add_new");

                if (maHS > 0)
                    Response.Redirect("ctbangkhaosat.aspx?phieuID=" + maHS.ToString() + "&isAdd=true");
                else
                    base.showMessage(true, "OK", "Dữ liệu không hợp lệ");
            }
            catch (Exception)
            {
                base.showMessage(true, "OK", "Có lỗi trong khi gửi dữ liệu!!!");
            }
            finally
            {
                base.hideLoader();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //this.addNewRecord();     
            try
            {
                long maHS = Int32.Parse(this.maPhieuKhaoSat.Value);
                String alertMessage = "";
                if (Request.QueryString["phieuID"] != null && maHS > 0)
                {
                    HO_SO_THAM_DINH hstd = ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().GetByMA_HO_SO(maHS);
                    if (hstd.ACTIVE_FLAG == "T")
                    {
                        this.saveRecord("save_from_temp");
                        alertMessage = "Thông tin về hồ sơ đã được cập nhật.";
                    }
                    else
                    {
                        maHS = this.addNewRecord("add_new_version");
                        alertMessage = "Hồ sơ đã được cập nhập và lưu thành một version mới.";
                    }


                     
                    base.hideLoader();
                    if (maHS > 0)
                        Response.Redirect("ctbangkhaosat.aspx?phieuID=" + maHS.ToString() + "&isAdd=true&msg=" + alertMessage);
                    else
                        base.showMessage(true, "OK", "Dữ liệu không hợp lệ");
                }
                else
                {
                    base.hideLoader();
                    base.showMessage(true, "OK", "Có lỗi trong quá trình cập nhật, vui lòng quay lại trang danh sách.");
                    Response.Redirect("dsbangkhaosat.aspx");
                }
            }
            catch (Exception)
            {
                base.showMessage(true, "OK", "Có lỗi trong khi gửi dữ liệu!!!");
            }
        }

        protected void btnSaveTemp_Click(object sender, EventArgs e)
        {
            //this.addNewRecord();     
            try
            {

                if (Request.QueryString["phieuID"] != null)
                    this.saveRecord("save_new_temp");
                else
                    this.addNewRecord("add_new_temp");
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()))
                    base.showMessage(true, "OK", "Bạn cần phải điền những dữ liệu bắt buộc sau để hồ sơ được lưu tạm.");
                else
                    base.showMessage(true, "OK", "Hồ sơ đã được lưu tạm.");
            }
            catch (Exception)
            {

                base.showMessage(true, "OK", "Có lỗi trong khi gửi dữ liệu!!!");
            }
            finally
            {
                base.hideLoader();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            long maPKS = Int32.Parse(this.maPhieuKhaoSat.Value);
            HO_SO_THAM_DINH hstd = ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().GetByMA_HO_SO(maPKS);
            hstd.ACTIVE_FLAG = "N";
            ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().Update(hstd);
            Response.Redirect("dsbangkhaosat.aspx");
        }

        #endregion

        #region loadSavedRecord

        private string loadSavedRecord(long maPKS)
        {
            string active_flag = "N";
            try
            {
                HO_SO_THAM_DINH hstd = ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().GetByMA_HO_SO(maPKS);
                if (hstd.ACTIVE_FLAG == "N")
                {
                    Response.Redirect("~/");
                }

                DUONG_KHAO_SAT dks = ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().GetByMA_DKS(hstd.MA_DKS);
                GIA_THAM_DINH gtd = ThamDinhGiaFacade.GetGIA_THAM_DINHBL().GetByMA_HO_SO(maPKS);
                THONG_TIN_VI_TRI ttvt = ThamDinhGiaFacade.GetTHONG_TIN_VI_TRIBL().GetByMA_HO_SO(maPKS);
                THUA_DAT td = ThamDinhGiaFacade.GetTHUA_DATBL().GetByMA_HO_SO(maPKS);
                CO_SO_HA_TANG csht = ThamDinhGiaFacade.GetCO_SO_HA_TANGBL().GetByMA_HO_SO(maPKS);
                CONG_TRINH_XAY_DUNG ctxd = ThamDinhGiaFacade.GetCONG_TRINH_XAY_DUNGBL().GetByMA_HO_SO(maPKS);
                VAT_LIEU_XAY_DUNG vlxd = ThamDinhGiaFacade.GetVAT_LIEU_XAY_DUNGBL().GetByMA_HO_SO(maPKS);

                loadDuongKhaoSat(dks);
                loadHoSoThamDinh(hstd);
                loadGiaThamDinh(gtd);
                loadThongTinViTri(ttvt);
                loadThuaDat(td);
                loadCoSoHaTang(csht);
                loadCongTrinhXayDung(ctxd);
                loadVatLieuXayDung(vlxd);
                active_flag = hstd.ACTIVE_FLAG;
                this._strUserCreate = hstd.NGUOI_KHOI_TAO.ToString();
            }
            catch (Exception)
            {
                active_flag = "N";
            }
            return active_flag;
        }

        private bool loadDuongKhaoSat(DUONG_KHAO_SAT dks)
        {
            bool isLoad = false;
            try
            {
                List<THAM_SO> lsThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
                lsThamSo = (from t in lsThamSo
                            where t.LOAI_THAM_SO == "LOAI_QUAN"
                            orderby t.SEQUENCE ascending
                            select t).ToList();
                this.ddlQuan.DataSource = lsThamSo;
                this.ddlQuan.DataBind();
                this.ddlQuan.SelectedIndex = this.ddlQuan.Items.IndexOf(this.ddlQuan.Items.FindByValue(dks.QUAN.ToString()));


                lsThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
                lsThamSo = (from t in lsThamSo
                            where t.LOAI_THAM_SO == "LOAI_PHUONG" && t.PARENT_ID == dks.QUAN
                            orderby t.SEQUENCE ascending
                            select t).ToList();
                this.ddlPhuong.DataSource = lsThamSo;
                this.ddlPhuong.DataBind();
                this.ddlPhuong.SelectedIndex = this.ddlPhuong.Items.IndexOf(this.ddlPhuong.Items.FindByValue(dks.PHUONG.ToString()));

                this.txtDuong.Text = dks.DUONG;
                this.txtDoanDuong.Text = dks.DOAN_DUONG;
                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadHoSoThamDinh(HO_SO_THAM_DINH hs)
        {
            bool isLoad = false;
            try
            {
                this.txtPhieuSo.Text = hs.SO_PHIEU;
                this.txtChuSuDung.Text = hs.TEN_CHU_SU_DUNG;
                this.txtDiaChiChuSuDung.Text = hs.DIA_CHI;
                this.txtViDo.Text = hs.GPS_LATITUDE.ToString();
                this.txtKinhDo.Text = hs.GPS_LONGTITUDE.ToString();
                isLoad = true;

                if (hs.TINH_TRANG_PHAP_LY == 3) // id giay huong trong bang tham so
                    this.cbxPLGiayHuong.Checked = true;
                else if (hs.TINH_TRANG_PHAP_LY == 4) // id giay do trong bang tham so
                    this.cbxPLGiayDo.Checked = true;
                else if (hs.TINH_TRANG_PHAP_LY == 5) // id giay hong trong bang tham so
                    this.cbxPLGiayHong.Checked = true;
                else if (hs.TINH_TRANG_PHAP_LY == 6) // id giay to khac trong bang tham so
                {
                    this.cbxPLKhac.Checked = true;
                    this.txtPLKhac.Text = hs.NOI_DUNG_PHAP_LY;
                    this.txtPLKhac.Enabled = true;
                }

                if (hs.LOAI_KHU_VUC_KINH_DOANH == 0) // khong co
                    this.cbxKVKDKhongCo.Checked = true;
                else if (hs.LOAI_KHU_VUC_KINH_DOANH == 1) // trung binh
                    this.cbxKVKDTrungBinh.Checked = true;
                else if (hs.LOAI_KHU_VUC_KINH_DOANH == 2) // sam uat
                    this.cbxKVKDSamUat.Checked = true;

                //cheat code dia chi su dung vo y kien rieng
                String yKienRieng = "";
                String diaChiChuSuDung_CHU = "";
                if (hs.Y_KIEN_THAM_DINH.Contains("<split>"))
                {
                    yKienRieng = hs.Y_KIEN_THAM_DINH.Substring(0, hs.Y_KIEN_THAM_DINH.IndexOf("<split>"));
                    diaChiChuSuDung_CHU = hs.Y_KIEN_THAM_DINH.Substring(hs.Y_KIEN_THAM_DINH.IndexOf("<split>") + 7);
                }
                else
                    yKienRieng = hs.Y_KIEN_THAM_DINH;
                this.txtYkienRieng.Text = yKienRieng;
                this.txtDiaChiChuSuDung_CHU.Text = diaChiChuSuDung_CHU;

                /*
                    hs.MA_DKS = dks.MA_DKS;
                 */
                this.CalendarExtender1.SelectedDate = hs.NGAY_KHOI_TAO;
                this.CalendarExtender2.SelectedDate = hs.NGAY_KHOI_TAO;
                this.txtThoiGianThuThap.Text = hs.NGAY_KHOI_TAO.ToShortDateString();

                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadGiaThamDinh(GIA_THAM_DINH gtd)
        {
            bool isLoad = false;
            try
            {
                if (gtd.GIA_GIAO_DICH_TRIEU_DONG > 0)
                {
                    this.ddlLoaiGiaGiaoDich.SelectedIndex = 0;
                    this.txtGiaGiaoDich.Text = gtd.GIA_GIAO_DICH_TRIEU_DONG.ToString();
                    this.txtGiaGiaoDich.Enabled = true;
                }
                else if (gtd.GIA_GIAO_DICH_CAY_VANG > 0)
                {
                    this.ddlLoaiGiaGiaoDich.SelectedIndex = 1;
                    this.txtGiaGiaoDich.Text = gtd.GIA_GIAO_DICH_CAY_VANG.ToString();
                    this.txtGiaGiaoDich.Enabled = true;
                }

                DateTime thoiDiemGiaoDich = gtd.THOI_DIEM_GIAO_DICH;
                this.ddlTDGDThang.SelectedIndex = this.ddlTDGDThang.Items.IndexOf(this.ddlTDGDThang.Items.FindByValue(thoiDiemGiaoDich.Month.ToString()));
                this.ddlTDGDNam.SelectedIndex = this.ddlTDGDNam.Items.IndexOf(this.ddlTDGDNam.Items.FindByValue(thoiDiemGiaoDich.Year.ToString()));

                if (gtd.LOAI_THANH_TOAN == 7) //lay tu bang THAM SO : Tra ngay
                    this.rbHTTTTraNgay.Checked = true;
                else if (gtd.LOAI_THANH_TOAN == 8) //lay tu bang THAM SO : Tra cham
                    this.rbHTTTTraCham.Checked = true;
                else if (gtd.LOAI_THANH_TOAN == 9) //lay tu bang THAM SO : Khac
                {
                    this.rbHTTTKhac.Checked = true;
                    this.txtHTTTKhac.Text = gtd.NOI_DUNG_THANH_TOAN;
                    this.txtHTTTKhac.Enabled = true;
                }
                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadThongTinViTri(THONG_TIN_VI_TRI ttvt)
        {
            bool isLoad = false;
            try
            {
                this.txtTTVTCachTrucDuongChinh.Text = ttvt.CACH_DUONG_CHINH.ToString();
                this.txtTTVTCachTrungTam.Text = ttvt.CACH_TRUNG_TAM.ToString();
                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadThuaDat(THUA_DAT td)
        {
            bool isLoad = false;
            try
            {
                this.txtTTTDatThuaSo.Text = td.SO_THUA_DAT;
                this.txtTTTDatToBanDo.Text = td.TO_BAN_DO;
                this.txtTTTDatDienTichDai.Text = td.CHIEU_DAI.ToString();
                this.txtTTTDatDienTichRong.Text = td.CHIEU_RONG.ToString();
                this.txtTTTDatDienTichTong.Text = td.TONG_DIEN_TICH.ToString();
                this.txtTTTDatQuiHoachCongNhan.Text = td.DIEN_TICH_CONG_NHAN.ToString();
                this.txtTTTDatQuiHoachKhongCongNhan.Text = td.DIEN_TICH_KHONG_CONG_NHAN.ToString();
                this.txtTTTDatQuiHoachKhongCongNhan.Text = td.DIEN_TICH_SU_DUNG_CHUNG.ToString();


                if (td.LOAI_HINH_DANG_THUA_DAT == 10) //lay tu bang tham so : Vuong
                    this.rbTTTDatHinhDangVuong.Checked = true;
                else if (td.LOAI_HINH_DANG_THUA_DAT == 11) //lay tu bang tham so : Top
                    this.rbTTTDatHinhDangTop.Checked = true;
                else if (td.LOAI_HINH_DANG_THUA_DAT == 12) //lay tu bang tham so : No hau
                    this.rbTTTDatHinhDangNoHau.Checked = true;
                else if (td.LOAI_HINH_DANG_THUA_DAT == 13)  //lay tu bang tham so : Khac
                {
                    this.rbTTTDatHinhDangKhac.Checked = true;
                    this.txtTTTDatHinhDangKhac.Text = td.HINH_DANG_THUA_DAT;
                    this.txtTTTDatHinhDangKhac.Enabled = true;
                }


                if (td.LOAI_DUONG_PHO == 14)
                    this.rbMatTien.Checked = true;             //lay tu bang tham so : mat tien
                else if (td.LOAI_DUONG_PHO == 15)
                    this.rbHem.Checked = true;             //lay tu bang tham so : hem

                this.txtTTTDatMatTienLoGioi.Text = td.LO_GIOI.ToString();
                this.txtTTTDatMatTienSoMT.Text = td.SO_MAT_TIEN.ToString();

                if (td.LOAI_LAN_DUONG == 16)
                    this.rbTTTDatMatTienMotChieu.Checked = true;             //lay tu bang tham so : mot chieu
                else if (td.LOAI_LAN_DUONG == 17)
                    this.rbTTTDatMatTienHaiChieu.Checked = true;             //lay tu bang tham so : hai chieu
                else if (td.LOAI_LAN_DUONG == 18)
                    this.rbTTTDatMatTienPhanCach.Checked = true;             //lay tu bang tham so : co dai phan cach


                //*********************************************************************************
                //IMPORTANT
                //LAY FIELD THUA_DAT.NOI_DUNG_LAN_DUONG LAM TEN NGUOI THU NHAP. 
                //*********************************************************************************
                this.txtTenNguoiThuThap.Text = td.NOI_DUNG_LAN_DUONG;


                if (td.LOAI_HEM == 19)
                    this.rbTTTDatDuongPhoCap1.Checked = true;             //lay tu bang tham so : hem cap 1
                else if (td.LOAI_HEM == 20)
                    this.rbTTTDatDuongPhoCap2.Checked = true;             //lay tu bang tham so : hem cap 2
                else if (td.LOAI_HEM == 21)
                    this.rbTTTDatDuongPhoCap3.Checked = true;             //lay tu bang tham so : hem cap 3
                else if (td.LOAI_HEM == 22)
                    this.rbTTTDatDuongPhoCap4.Checked = true;             //lay tu bang tham so : hem cap 4

                if (td.LOAI_HEM == 23)
                    this.rbTTTHemCapKhacCap1.Checked = true;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 1
                else if (td.LOAI_HEM == 24)
                    this.rbTTTHemCapKhacCap2.Checked = true;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 2
                else if (td.LOAI_HEM == 25)
                    this.rbTTTHemCapKhacCap3.Checked = true;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 3
                else if (td.LOAI_HEM == 26)
                    this.rbTTTHemCapKhacCap4.Checked = true;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 4

                if (td.LOAI_HEM >= 19 && td.LOAI_HEM <= 22)
                    this.rbHemCap1.Checked = true;
                if (td.LOAI_HEM >= 23 && td.LOAI_HEM <= 26)
                    this.rbHemCapKhac.Checked = true;
                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadCoSoHaTang(CO_SO_HA_TANG csht)
        {
            bool isLoad = false;
            try
            {
                //1: Co, 2: Khong:
                //1: Nhieu, 2 Trung binh, 3 It
                //1: Nhieu, 2 Trung binh, 3 It, 4 Khong co
                if (csht.DONG_HO_DIEN == 1)
                    this.cbxTTHTDienDHDienCo.Checked = true;
                else
                    this.cbxTTHTDienDHDienKhong.Checked = true;

                if (csht.CAP_TREO == 1)
                    this.cbxTTHTDienCapTreoCo.Checked = true;
                else
                    this.cbxTTHTDienCapTreoKhong.Checked = true;

                if (csht.CAP_NGAM == 1)
                    this.cbxTTHTDienCapNgamCo.Checked = true;
                else
                    this.cbxTTHTDienCapNgamKhong.Checked = true;

                if (csht.DONG_HO_NUOC == 1)
                    this.cbxTTHTDienDHDienCo.Checked = true;
                else
                    this.cbxTTHTDienDHDienKhong.Checked = true;

                if (csht.AP_LUC_NUOC == 1)
                    this.cbxTTHTNuocALNuocManh.Checked = true;
                else
                    this.cbxTTHTNuocALNuocYeu.Checked = true;

                if (csht.CHAT_LUONG_NUOC == 1)
                    this.cbxTTHTNuocCLNuocSach.Checked = true;
                else
                    this.cbxTTHTNuocCLNuocDo.Checked = true;

                if (csht.TIENG_ON == 1)
                    this.cbxTTHTMTTiengOnCo.Checked = true;
                else
                    this.cbxTTHTMTTiengOnKhong.Checked = true;

                if (csht.KHOI_BUI == 1)
                    this.cbxTTHTMTKhoiBuiNhieu.Checked = true;
                else if (csht.KHOI_BUI == 2)
                    this.cbxTTHTMTKhoiBuiTrungBinh.Checked = true;
                else
                    this.cbxTTHTMTKhoiBuiIt.Checked = true;

                if (csht.TINH_TRANG_NGAP == 1)
                    this.cbxTTHTMTTTNgapNhieu.Checked = true;
                else if (csht.TINH_TRANG_NGAP == 2)
                    this.cbxTTHTMTTTNgapTrungBinh.Checked = true;
                else if (csht.TINH_TRANG_NGAP == 3)
                    this.cbxTTHTMTTTNgapIt.Checked = true;
                else
                    this.cbxTTHTMTTTNgapKhongCo.Checked = true;

                if (csht.HE_THONG_CHIEU_SANG == 1)
                    this.cbxTTHTANTTChieuSangCo.Checked = true;
                else
                    this.cbxTTHTANTTChieuSangKhong.Checked = true;

                if (csht.CHOT_BAO_VE == 1)
                    this.cbxTTHTANTTDanPhongCo.Checked = true;
                else
                    this.cbxTTHTANTTDanPhongKhong.Checked = true;

                if (csht.AN_NINH == 1)
                    this.cbxTTHTANTTTot.Checked = true;
                else if (csht.AN_NINH == 2)
                    this.cbxTTHTANTTTrungBinh.Checked = true;
                else
                    this.cbxTTHTANTTKem.Checked = true;

                if (csht.GAN_CO_SO_GIAO_DUC == 1)
                    this.cbxYKKhacCSGDCo.Checked = true;
                else
                    this.cbxYKKhacCSGDKhong.Checked = true;

                if (csht.GAN_CO_SO_Y_TE == 1)
                    this.cbxYKKhacCSYTeCo.Checked = true;
                else
                    this.cbxYKKhacCSYTeKhong.Checked = true;

                if (csht.GAN_CHO_TRUNG_TAM == 1)
                    this.cbxYKKhacTTTMCo.Checked = true;
                else
                    this.cbxYKKhacTTTMKhong.Checked = true;

                if (csht.GAN_CO_SO_GIAI_TRI == 1)
                    this.cbxYKKhacCSGiaiTriCo.Checked = true;
                else
                    this.cbxYKKhacCSGiaiTriKhong.Checked = true;

                if (csht.GAN_DICH_VU_CONG_CONG == 1)
                    this.cbxYKKhacDVCCCo.Checked = true;
                else
                    this.cbxYKKhacDVCCKhong.Checked = true;

                if (csht.GAN_THONG_TIN_LIEN_LAC == 1)
                    this.cbxYKKhacTTLLCo.Checked = true;
                else
                    this.cbxYKKhacTTLLKhong.Checked = true;

                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadCongTrinhXayDung(CONG_TRINH_XAY_DUNG ctxd)
        {
            bool isLoad = false;
            try
            {
                if (ctxd.LOAI_NHA == 23)
                    this.rbTTCTLoaiNhaBietThu.Checked = true;                   //lay tu bang THAM SO: biet thu
                else if (ctxd.LOAI_NHA == 24)
                    this.rbTTCTLoaiNhaPho.Checked = true;                      //lay tu bang THAM SO: pho
                else if (ctxd.LOAI_NHA == 25)
                    this.rbTTCTLoaiNhaLienKe.Checked = true;                    //lay tu bang THAM SO: lien ke
                else if (ctxd.LOAI_NHA == 26)
                    this.rbTTCTLoaiNhaChungCu.Checked = true;                   //lay tu bang THAM SO: chung cu
                else if (ctxd.LOAI_NHA == 27)                                   //lay tu bang THAM SO: khac
                {
                    this.rbTTCTLoaiNhaKhac.Checked = true;
                    this.txtTTCTLoaiNhaChungChuKhac.Text = ctxd.NOI_DUNG_LOAI_NHA;
                    this.txtTTCTLoaiNhaChungChuKhac.Enabled = true;
                }

                if (ctxd.LOAI_CAP_NHA == 28)
                    this.rbTTCTCapNha1.Checked = true;                 //lay tu bang THAM SO: cap 1
                else if (ctxd.LOAI_CAP_NHA == 29)
                    this.rbTTCTCapNha2.Checked = true;                 //lay tu bang THAM SO: cap 2
                else if (ctxd.LOAI_CAP_NHA == 30)
                    this.rbTTCTCapNha3.Checked = true;                 //lay tu bang THAM SO: cap 3
                else if (ctxd.LOAI_CAP_NHA == 31)
                    this.rbTTCTCapNha4.Checked = true;                 //lay tu bang THAM SO: cap 4
                else if (ctxd.LOAI_CAP_NHA == 32)
                    this.rbTTCTCapNhaTam.Checked = true;                 //lay tu bang THAM SO: khac   

                if (ctxd.HUONG_NHA == 33)
                    this.rbTTCTHuongNhaDong.Checked = true;                 //lay tu bang THAM SO: D
                else if (ctxd.HUONG_NHA == 34)
                    this.rbTTCTHuongNhaDongBac.Checked = true;                 //lay tu bang THAM SO: DB
                else if (ctxd.HUONG_NHA == 35)
                    this.rbTTCTHuongNhaTay.Checked = true;                 //lay tu bang THAM SO: T
                else if (ctxd.HUONG_NHA == 36)
                    this.rbTTCTHuongNhaTayBac.Checked = true;                 //lay tu bang THAM SO: TB
                if (ctxd.HUONG_NHA == 37)
                    this.rbTTCTHuongNhaNam.Checked = true;                 //lay tu bang THAM SO: N
                else if (ctxd.HUONG_NHA == 38)
                    this.rbTTCTHuongNhaDongNam.Checked = true;                 //lay tu bang THAM SO: DN
                else if (ctxd.HUONG_NHA == 39)
                    this.rbTTCTHuongNhaBac.Checked = true;                 //lay tu bang THAM SO: B
                else if (ctxd.HUONG_NHA == 40)
                    this.rbTTCTHuongNhaTayNam.Checked = true;                 //lay tu bang THAM SO: TN


                if (ctxd.GIAY_PHEP_XAY_DUNG == "Y")
                    this.rbTTCTGiayPhepCo.Checked = true;
                else if (ctxd.GIAY_PHEP_XAY_DUNG == "N")
                    this.rbTTCTGiayPhepKhong.Checked = true;

                this.txtTTCTDienTichCongNhan.Text = ctxd.DIEN_TICH_XD_CONG_NHAN.ToString();
                this.txtTTCTDienTichCongNhan.Text = ctxd.DIEN_TICH_XD_KHONG_CONG_NHAN.ToString();
                this.txtTTCTNamXayDungXong.Text = ctxd.NAM_XAY_DUNG.Year.ToString();
                this.txtTTCTSoNamSuDung.Text = ctxd.SO_NAM_SU_DUNG.ToString();
                this.txtTTCTDonGiaBinhQuan.Text = ctxd.DON_GIA_XAY_DUNG.ToString();
                this.txtTTCTTuoiDoi.Text = ctxd.TUOI_DOI_KINH_TE.ToString();
                this.txtTTCTGiaTriXayDung.Text = ctxd.GIA_TRI_CONG_TRINH.ToString(); ;
                this.txtTTCTSoLanSuaChua.Text = ctxd.SO_LAN_SUA_CHUA.ToString();
                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        private bool loadVatLieuXayDung(VAT_LIEU_XAY_DUNG vlxd)
        {
            bool isLoad = false;
            try
            {

                if (vlxd.LOAI_KET_CAU_KHUNG == 41)
                    this.rbTTCTVatLieuKhungBTCT.Checked = true;                 //lay tu bang THAM SO: BTCT
                else if (vlxd.LOAI_KET_CAU_KHUNG == 42)
                    this.rbTTCTVatLieuKhungGach.Checked = true;              //lay tu bang THAM SO: Gach
                else if (vlxd.LOAI_KET_CAU_KHUNG == 43)
                    this.rbTTCTVatLieuKhungGo.Checked = true;                 //lay tu bang THAM SO: Go
                else if (vlxd.LOAI_KET_CAU_KHUNG == 44)
                {
                    this.rbTTCTVatLieuKhungKhac.Checked = true;                 //lay tu bang THAM SO: khac
                    this.txtTTCTVatLieuKhungKhac.Text = vlxd.NOI_DUNG_KET_CAU_KHUNG;
                    this.txtTTCTVatLieuKhungKhac.Enabled = true;
                }

                if (vlxd.LOAI_KET_CAU_MONG == 45)
                    this.rbTTCTVatLieuMongCoc.Checked = true;                 //lay tu bang THAM SO: Coc
                else if (vlxd.LOAI_KET_CAU_MONG == 46)
                    this.rbTTCTVatLieuMongBang.Checked = true;                 //lay tu bang THAM SO: Bang
                else if (vlxd.LOAI_KET_CAU_MONG == 47)
                    this.rbTTCTVatLieuMongBe.Checked = true;                 //lay tu bang THAM SO: Be
                else if (vlxd.LOAI_KET_CAU_MONG == 48)
                {
                    this.rbTTCTVatLieuMongKhac.Checked = true;                 //lay tu bang THAM SO: khac
                    this.txtTTCTVatLieuMongKhac.Text = vlxd.NOI_DUNG_KET_CAU_MONG;
                    this.txtTTCTVatLieuMongKhac.Enabled = true;
                }

                if (vlxd.LOAI_KET_CAU_TUONG_NHA == 49)
                    this.rbTTCTVatLieuTuongGach.Checked = true;                 //lay tu bang THAM SO: Gacg
                else if (vlxd.LOAI_KET_CAU_TUONG_NHA == 50)
                    this.rbTTCTVatLieuTuongGo.Checked = true;                 //lay tu bang THAM SO: Go
                else if (vlxd.LOAI_KET_CAU_TUONG_NHA == 51)
                    this.rbTTCTVatLieuTuongTole.Checked = true;                 //lay tu bang THAM SO: Tole
                else if (vlxd.LOAI_KET_CAU_TUONG_NHA == 52)
                {
                    this.rbTTCTVatLieuTuongKhac.Checked = true;                 //lay tu bang THAM SO: khac
                    this.txtTTCTVatLieuTuongKhac.Text = vlxd.NOI_DUNG_KET_CAU_TUONG_NHA;
                    this.txtTTCTVatLieuTuongKhac.Enabled = true;
                }

                if (vlxd.LOAI_KET_CAU_NEN == 53)
                    this.rbTTCTVatLieuNenGach.Checked = true;                 //lay tu bang THAM SO: Gacg
                else if (vlxd.LOAI_KET_CAU_NEN == 54)
                    this.rbTTCTVatLieuNenGo.Checked = true;                 //lay tu bang THAM SO: Go
                else if (vlxd.LOAI_KET_CAU_NEN == 55)
                    this.rbTTCTVatLieuNenXiMang.Checked = true;                 //lay tu bang THAM SO: Xi mang
                else if (vlxd.LOAI_KET_CAU_NEN == 56)
                {
                    this.rbTTCTVatLieuNenKhac.Checked = true;                 //lay tu bang THAM SO: khac
                    this.txtrbTTCTVatLieuNenKhac.Text = vlxd.NOI_DUNG_KET_CAU_NEN;
                    this.txtrbTTCTVatLieuNenKhac.Enabled = true;
                }

                if (vlxd.LOAI_KET_CAU_MAI == 57)
                    this.rbTTCTVatLieuMaiBang.Checked = true;                 //lay tu bang THAM SO: Bang
                else if (vlxd.LOAI_KET_CAU_MAI == 58)
                    this.rbTTCTVatLieuMaiNgoi.Checked = true;                 //lay tu bang THAM SO: Ngoi
                else if (vlxd.LOAI_KET_CAU_MAI == 59)
                    this.rbTTCTVatLieuMaiTole.Checked = true;                 //lay tu bang THAM SO: Tole
                else if (vlxd.LOAI_KET_CAU_MAI == 60)
                {
                    this.rbTTCTVatLieuMaiKhac.Checked = true;                 //lay tu bang THAM SO: khac
                    this.txtTTCTVatLieuMaiKhac.Text = vlxd.NOI_DUNG_KET_CAU_MAI;
                    this.txtTTCTVatLieuMaiKhac.Enabled = true;
                }

                this.txtTTCTVatLieuChatLuongMong.Text = vlxd.CHAT_LUONG_MONG.ToString();
                this.txtTTCTVatLieuChatLuongSan.Text = vlxd.CHAT_LUONG_SAN.ToString();
                this.txtTTCTVatLieuChatLuongTuong.Text = vlxd.CHAT_LUONG_TUONG.ToString();
                this.txtTTCTVatLieuChatLuongTran.Text = vlxd.CHAT_LUONG_TRAN.ToString();
                this.txtTTCTVatLieuChatLuongMai.Text = vlxd.CHAT_LUONG_MAI.ToString();

                this.txtTTCTVatLieuTiLeKetCauMong.Text = vlxd.TI_LE_KET_CAU_MONG.ToString();
                this.txtTTCTVatLieuTiLeKetCauSan.Text = vlxd.TI_LE_KET_CAU_SAN.ToString();
                this.txtTTCTVatLieuTiLeKetCauTuong.Text = vlxd.TI_LE_KET_CAU_TUONG.ToString();
                this.txtTTCTVatLieuTiLeKetCauTran.Text = vlxd.TI_LE_KET_CAU_TRAN.ToString();
                this.txtTTCTVatLieuTiLeKetCauMai.Text = vlxd.TI_LE_KET_CAU_MAI.ToString();
                this.txtTTCTVatLieuSoPhongNgu.Text = vlxd.SO_PHONG_NGU.ToString();
                this.txtTTCTVatLieuSoNhaVS.Text = vlxd.SO_NHA_VE_SINH.ToString();

                if (vlxd.LOAI_TRANG_TRI_NOI_THAT == 61)
                    this.rbTTCTVatLieuTTNTCaoCap.Checked = true;                 //lay tu bang THAM SO: cao cap
                else if (vlxd.LOAI_TRANG_TRI_NOI_THAT == 62)
                    this.rbTTCTVatLieuTTNTKha.Checked = true;                 //lay tu bang THAM SO: kha
                else if (vlxd.LOAI_TRANG_TRI_NOI_THAT == 63)
                    this.rbTTCTVatLieuTTNTTrungBinh.Checked = true;                 //lay tu bang THAM SO: trung binh
                else if (vlxd.LOAI_TRANG_TRI_NOI_THAT == 64)
                {
                    this.rbTTCTVatLieuTTNTTam.Checked = true;                 //lay tu bang THAM SO: tam                
                }

                if (vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.Contains("65"))
                    this.rbTTCTVatLieuCongTrinhSanThuong.Checked = true;                 //lay tu bang THAM SO: san thuong
                if (vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.Contains("66"))
                    this.rbTTCTVatLieuCongTrinhHoBoi.Checked = true;                 //lay tu bang THAM SO: ho boi
                if (vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.Contains("67"))
                    this.rbTTCTVatLieuCongTrinhSan.Checked = true;                 //lay tu bang THAM SO: san
                if (vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.Contains("68"))
                {
                    this.rbTTCTVatLieuCongTrinhKhac.Checked = true;                 //lay tu bang THAM SO: khac
                    int firstIndex = vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.IndexOf("<khac>");
                    int lastIndex = vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.LastIndexOf("<khac>");
                    String noiDung = vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG.Substring(firstIndex + 6, lastIndex - firstIndex - 6);
                    txtTTCTVatLieuCongTrinhKhac.Text = noiDung;
                    this.txtTTCTVatLieuCongTrinhKhac.Enabled = true;
                }
                isLoad = true;
            }
            catch (Exception)
            {
                isLoad = false;
            }
            return isLoad;
        }

        #endregion

        #region AddSaveNewRecord

        private long saveRecord(String addType)
        {
            this.lblErrorMessge.Text = "";
            long maHS = -1;
            DateTime dtCreated = DateTime.Now;
            DUONG_KHAO_SAT dks = null;
            HO_SO_THAM_DINH hstd = null;
            GIA_THAM_DINH gtd = null;
            THONG_TIN_VI_TRI ttvt = null;
            THUA_DAT td = null;
            CO_SO_HA_TANG csht = null;
            CONG_TRINH_XAY_DUNG ctxd = null;
            VAT_LIEU_XAY_DUNG vlxd = null;
            try
            {
                dks = buildDuongKhaoSat();
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()))
                    throw new Exception();
                long maDKS = ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().Add(dks);
                dks = ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().GetByMA_DKS(maDKS);

                maHS = Int32.Parse(this.maPhieuKhaoSat.Value);
                hstd = buildHoSoThamDinh(dks);
                hstd.MA_HO_SO = maHS;
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()))
                    throw new Exception();
                if ("save_new_temp".Equals(addType))
                {
                    hstd.ACTIVE_FLAG = "T";
                }
                else if ("save_from_temp".Equals(addType))
                {
                    hstd.ACTIVE_FLAG = "Y";
                }

                gtd = buildGiaThamDinh(hstd);
                ttvt = buildThongTinViTri(hstd);
                td = buildThuaDat(hstd);
                csht = buildCoSoHaTang(hstd);
                ctxd = buildCongTrinhXayDung(hstd);
                vlxd = buildVatLieuXayDung(hstd);
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()))
                    throw new Exception();
                bool isTinhGia = TinhGia.TinhGiaDat(ref hstd, td, gtd, ctxd, vlxd);
                if (!isTinhGia)
                {
                    this.lblErrorMessge.Text = "Dữ liệu nhập không đủ để Tính Giá!!!";
                    throw new Exception();
                }
                ThamDinhGiaFacade.GetGIA_THAM_DINHBL().Update(gtd);
                ThamDinhGiaFacade.GetTHONG_TIN_VI_TRIBL().Update(ttvt);
                ThamDinhGiaFacade.GetTHUA_DATBL().Update(td);
                ThamDinhGiaFacade.GetCO_SO_HA_TANGBL().Update(csht);
                ThamDinhGiaFacade.GetCONG_TRINH_XAY_DUNGBL().Update(ctxd);
                ThamDinhGiaFacade.GetVAT_LIEU_XAY_DUNGBL().Update(vlxd);
                ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().Update(hstd);
            }
            catch (Exception)
            {
                maHS = -1;
                if (dks != null && dks.NGAY_KHOI_TAO.CompareTo(dtCreated) >= 0)
                    ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().Delete(dks.MA_DKS);
            }
            return maHS;
        }

        private long addNewRecord(String addType)
        {
            this.lblErrorMessge.Text = "";
            long maHS = -1;
            DateTime dtCreated = DateTime.Now;
            DUONG_KHAO_SAT dks = null;
            HO_SO_THAM_DINH hstd = null;
            GIA_THAM_DINH gtd = null;
            THONG_TIN_VI_TRI ttvt = null;
            THUA_DAT td = null;
            CO_SO_HA_TANG csht = null;
            CONG_TRINH_XAY_DUNG ctxd = null;
            VAT_LIEU_XAY_DUNG vlxd = null;
            try
            {
                dks = buildDuongKhaoSat();
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()) && !"add_new_temp".Equals(addType))
                    return -1;
                long maDKS = ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().Add(dks);
                dks = ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().GetByMA_DKS(maDKS);
                dks.MA_DKS = maDKS;


                hstd = buildHoSoThamDinh(dks);
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()))
                    throw new Exception();
                if ("add_new".Equals(addType))
                    hstd.ACTIVE_FLAG = "Y";
                if ("add_new_version".Equals(addType))
                    hstd.ACTIVE_FLAG = "Y";
                if ("add_new_temp".Equals(addType))
                {
                    hstd.ACTIVE_FLAG = "T";
                }

                maHS = ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().Add(hstd);
                hstd.MA_HO_SO = maHS;

                gtd = buildGiaThamDinh(hstd);
                ttvt = buildThongTinViTri(hstd);
                td = buildThuaDat(hstd);
                csht = buildCoSoHaTang(hstd);
                ctxd = buildCongTrinhXayDung(hstd);
                vlxd = buildVatLieuXayDung(hstd);
                if (!String.Empty.Equals(this.lblErrorMessge.Text.Trim()) && !"add_new_temp".Equals(addType))
                    throw new Exception();
                ThamDinhGiaFacade.GetGIA_THAM_DINHBL().Add(gtd);
                ThamDinhGiaFacade.GetTHONG_TIN_VI_TRIBL().Add(ttvt);
                ThamDinhGiaFacade.GetTHUA_DATBL().Add(td);
                ThamDinhGiaFacade.GetCO_SO_HA_TANGBL().Add(csht);
                ThamDinhGiaFacade.GetCONG_TRINH_XAY_DUNGBL().Add(ctxd);
                ThamDinhGiaFacade.GetVAT_LIEU_XAY_DUNGBL().Add(vlxd);
                if (!"add_new_temp".Equals(addType))
                {
                    bool isTinhGia = TinhGia.TinhGiaDat(ref hstd, td, gtd, ctxd, vlxd);
                    if (!isTinhGia)
                    {
                        this.lblErrorMessge.Text = "Dữ liệu nhập không hợp lệ để Tính Giá!!! Bạn có thể lưu tạm để sửa sau.";
                        throw new Exception();
                    }
                    ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().Update(hstd);
                }
            }
            catch (Exception ex)
            {
                maHS = -1;
                if (gtd != null)
                    ThamDinhGiaFacade.GetGIA_THAM_DINHBL().Delete(hstd.MA_HO_SO);
                if (ttvt != null)
                    ThamDinhGiaFacade.GetTHONG_TIN_VI_TRIBL().Delete(hstd.MA_HO_SO);
                if (td != null)
                    ThamDinhGiaFacade.GetTHUA_DATBL().Delete(hstd.MA_HO_SO);
                if (csht != null)
                    ThamDinhGiaFacade.GetCO_SO_HA_TANGBL().Delete(hstd.MA_HO_SO);
                if (ctxd != null)
                    ThamDinhGiaFacade.GetCONG_TRINH_XAY_DUNGBL().Delete(hstd.MA_HO_SO);
                if (vlxd != null)
                    ThamDinhGiaFacade.GetVAT_LIEU_XAY_DUNGBL().Delete(hstd.MA_HO_SO);
                if (hstd != null)
                    ThamDinhGiaFacade.GetHO_SO_THAM_DINHBL().Delete(hstd.MA_HO_SO);
                if (dks != null && dks.NGAY_KHOI_TAO.CompareTo(dtCreated) >= 0)
                    ThamDinhGiaFacade.GetDUONG_KHAO_SATBL().Delete(dks.MA_DKS);
            }
            return maHS;
        }

        private DUONG_KHAO_SAT buildDuongKhaoSat()
        {
            DUONG_KHAO_SAT dks = new DUONG_KHAO_SAT();
            dks.QUAN = Int32.Parse(this.ddlQuan.SelectedValue);
            dks.PHUONG = Int32.Parse(this.ddlPhuong.SelectedValue);

            if (!String.Empty.Equals(this.txtDuong.Text.Trim()))
                dks.DUONG = this.txtDuong.Text.Trim();
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Đường' cần khảo sát !<br>";
                this.txtDuong.Style.Add("background-color", "red");
            }

            dks.DOAN_DUONG = this.txtDoanDuong.Text.Trim();
            dks.NGAY_KHOI_TAO = DateTime.Now;
            dks.ACTIVE_FLAG = "Y";
            return dks;
        }

        private HO_SO_THAM_DINH buildHoSoThamDinh(DUONG_KHAO_SAT dks)
        {
            HO_SO_THAM_DINH hs = new HO_SO_THAM_DINH();
            hs.MA_DKS = dks.MA_DKS;
            if (!String.Empty.Equals(this.txtPhieuSo.Text.Trim()))
                hs.SO_PHIEU = this.txtPhieuSo.Text.Trim();
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Phiếu Số' cần khảo sát !<br>";
                this.txtPhieuSo.Style.Add("background-color", "red");
            }

            if (!String.Empty.Equals(this.txtChuSuDung.Text.Trim()))
                hs.TEN_CHU_SU_DUNG = this.txtChuSuDung.Text.Trim();
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Chủ Sử Dụng' cần khảo sát !<br>";
                this.txtChuSuDung.Style.Add("background-color", "red");
            }

            if (!String.Empty.Equals(this.txtDiaChiChuSuDung.Text.Trim()))
                hs.DIA_CHI = this.txtDiaChiChuSuDung.Text.Trim();
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Địa chỉ Bất Động Sản' cần khảo sát !<br>";
                this.txtDiaChiChuSuDung.Style.Add("background-color", "red");
            }

            if (!String.Empty.Equals(this.txtViDo.Text.Trim()) && !String.Empty.Equals(this.txtKinhDo.Text.Trim()))
            {
                hs.GPS_LATITUDE = Double.Parse(this.txtViDo.Text.Trim());
                hs.GPS_LONGTITUDE = Double.Parse(this.txtKinhDo.Text.Trim());
                hs.GPS_CHI_CHU = hs.TEN_CHU_SU_DUNG + " : " + hs.DIA_CHI;
            }
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa chọn vị trí cần khảo sát trên bản đồ. !<br>";
                this.txtDiaChiChuSuDung.Style.Add("background-color", "red");
            }

            hs.NOI_DUNG_PHAP_LY = "";
            if (this.cbxPLGiayHuong.Checked)
                hs.TINH_TRANG_PHAP_LY = 3; // id giay huong trong bang tham so
            else if (this.cbxPLGiayDo.Checked)
                hs.TINH_TRANG_PHAP_LY = 4; // id giay do trong bang tham so
            else if (this.cbxPLGiayHong.Checked)
                hs.TINH_TRANG_PHAP_LY = 5; // id giay hong trong bang tham so
            else if (this.cbxPLKhac.Checked)
            {
                hs.TINH_TRANG_PHAP_LY = 6; // id giay to khac trong bang tham so
                if (!String.Empty.Equals(this.txtPLKhac.Text.Trim()))
                    hs.NOI_DUNG_PHAP_LY = this.txtPLKhac.Text.Trim();
                else
                {
                    this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Tình trạng pháp lý KHÁC' cần khảo sát !<br>";
                    this.txtPLKhac.Style.Add("background-color", "red");
                }
            }

            if (this.cbxKVKDKhongCo.Checked)
                hs.LOAI_KHU_VUC_KINH_DOANH = 0; // khong co
            else if (this.cbxKVKDTrungBinh.Checked)
                hs.LOAI_KHU_VUC_KINH_DOANH = 1; // trung binh
            else if (this.cbxKVKDSamUat.Checked)
                hs.LOAI_KHU_VUC_KINH_DOANH = 2; // sam uat

            hs.Y_KIEN_THAM_DINH = this.txtYkienRieng.Text.Trim();

            if (!String.Empty.Equals(this.txtDiaChiChuSuDung_CHU.Text.Trim()))
                hs.Y_KIEN_THAM_DINH += "<split>" + this.txtDiaChiChuSuDung_CHU.Text.Trim();
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Địa chỉ Chủ Sử Dụng' cần khảo sát !<br>";
                this.txtDiaChiChuSuDung_CHU.Style.Add("background-color", "red");
            }

            System.Web.Security.MembershipUser userLogin = wsRK.AppUser.MembershipDataservice.getUser(User.Identity.Name);
            Guid userID = (Guid)userLogin.ProviderUserKey;
            hs.NGUOI_KHOI_TAO = userID;
            if (!String.Empty.Equals(this.txtThoiGianThuThap.Text.Trim()))
                hs.NGAY_KHOI_TAO = DateTime.Parse(this.txtThoiGianThuThap.Text.Trim());
            else
                hs.NGAY_KHOI_TAO = DateTime.Now;
            hs.NGAY_CAP_NHAT = DateTime.Now;
            hs.ACTIVE_FLAG = "Y";
            return hs;
        }

        private GIA_THAM_DINH buildGiaThamDinh(HO_SO_THAM_DINH hstd)
        {
            GIA_THAM_DINH gtd = new GIA_THAM_DINH();
            gtd.MA_HO_SO = hstd.MA_HO_SO;
            if (!String.Empty.Equals(this.txtGiaGiaoDich.Text.Trim()))
            {
                //all value is trieu dong
                if (this.ddlLoaiGiaGiaoDich.Items[0].Selected)
                    gtd.GIA_GIAO_DICH_TRIEU_DONG = Double.Parse(this.txtGiaGiaoDich.Text.Trim());
                else
                    gtd.GIA_GIAO_DICH_CAY_VANG = Double.Parse(this.txtGiaGiaoDich.Text.Trim());
            }
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Giá giao dịch' cần khảo sát !<br>";
                this.txtGiaGiaoDich.Style.Add("background-color", "red");
            }

            int month = Int16.Parse(this.ddlTDGDThang.SelectedValue);
            int year = Int16.Parse(this.ddlTDGDNam.SelectedValue);
            DateTime thoiDiemGiaoDich = new DateTime(year, month, 1);
            gtd.THOI_DIEM_GIAO_DICH = thoiDiemGiaoDich;

            gtd.NOI_DUNG_THANH_TOAN = "";
            if (this.rbHTTTTraNgay.Checked)
                gtd.LOAI_THANH_TOAN = 7; //lay tu bang THAM SO : Tra ngay
            else if (this.rbHTTTTraCham.Checked)
                gtd.LOAI_THANH_TOAN = 8; //lay tu bang THAM SO : Tra cham
            else if (this.rbHTTTKhac.Checked)
            {
                gtd.LOAI_THANH_TOAN = 9; //lay tu bang THAM SO : Khac
                gtd.NOI_DUNG_THANH_TOAN = this.txtHTTTKhac.Text.Trim();
            }
            return gtd;
        }

        private THONG_TIN_VI_TRI buildThongTinViTri(HO_SO_THAM_DINH hstd)
        {
            THONG_TIN_VI_TRI ttvt = new THONG_TIN_VI_TRI();
            ttvt.MA_HO_SO = hstd.MA_HO_SO;
            if (!String.Empty.Equals(this.txtTTVTCachTrucDuongChinh.Text.Trim()))
                ttvt.CACH_DUONG_CHINH = Double.Parse(this.txtTTVTCachTrucDuongChinh.Text.Trim());
            if (!String.Empty.Equals(this.txtTTVTCachTrungTam.Text.Trim()))
                ttvt.CACH_TRUNG_TAM = Double.Parse(this.txtTTVTCachTrungTam.Text.Trim());
            return ttvt;
        }

        private THUA_DAT buildThuaDat(HO_SO_THAM_DINH hstd)
        {
            THUA_DAT td = new THUA_DAT();
            td.MA_HO_SO = hstd.MA_HO_SO;

            //if (!String.Empty.Equals(this.txtTTTDatThuaSo.Text.Trim()))
            td.SO_THUA_DAT = this.txtTTTDatThuaSo.Text.Trim();
            //else
            //{
            //    this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Số thửa đất' cần khảo sát !<br>";
            //    this.txtTTTDatThuaSo.Style.Add("background-color", "red");
            //}

            //if (!String.Empty.Equals(this.txtTTTDatToBanDo.Text.Trim()))
            td.TO_BAN_DO = this.txtTTTDatToBanDo.Text.Trim();
            //else
            //{
            //    this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Tờ bản đồ' cần khảo sát !<br>";
            //    this.txtTTTDatToBanDo.Style.Add("background-color", "red");
            //}


            if (!String.Empty.Equals(this.txtTTTDatDienTichDai.Text.Trim()))
                td.CHIEU_DAI = Double.Parse(this.txtTTTDatDienTichDai.Text.Trim());
            if (!String.Empty.Equals(this.txtTTTDatDienTichRong.Text.Trim()))
                td.CHIEU_RONG = Double.Parse(this.txtTTTDatDienTichRong.Text.Trim());
            if (!String.Empty.Equals(this.txtTTTDatDienTichTong.Text.Trim()))
                td.TONG_DIEN_TICH = Double.Parse(this.txtTTTDatDienTichTong.Text.Trim());
            if (!String.Empty.Equals(this.txtTTTDatQuiHoachCongNhan.Text.Trim()))
                td.DIEN_TICH_CONG_NHAN = Double.Parse(this.txtTTTDatQuiHoachCongNhan.Text.Trim());
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Diện Tích Công Nhận' cần khảo sát !<br>";
                this.txtTTTDatQuiHoachCongNhan.Style.Add("background-color", "red");
            }
            if (!String.Empty.Equals(this.txtTTTDatQuiHoachKhongCongNhan.Text.Trim()))
                td.DIEN_TICH_KHONG_CONG_NHAN = Double.Parse(this.txtTTTDatQuiHoachKhongCongNhan.Text.Trim());
            if (!String.Empty.Equals(this.txtTTTDatQuiHoachKhongCongNhan.Text.Trim()))
                td.DIEN_TICH_SU_DUNG_CHUNG = Double.Parse(this.txtTTTDatQuiHoachKhongCongNhan.Text.Trim());

            td.HINH_DANG_THUA_DAT = "";
            if (this.rbTTTDatHinhDangVuong.Checked)
                td.LOAI_HINH_DANG_THUA_DAT = 10;             //lay tu bang tham so : Vuong
            else if (this.rbTTTDatHinhDangTop.Checked)
                td.LOAI_HINH_DANG_THUA_DAT = 11;             //lay tu bang tham so : Top
            else if (this.rbTTTDatHinhDangNoHau.Checked)
                td.LOAI_HINH_DANG_THUA_DAT = 12;             //lay tu bang tham so : No hau
            else if (this.rbTTTDatHinhDangKhac.Checked)
            {
                td.LOAI_HINH_DANG_THUA_DAT = 13;             //lay tu bang tham so : Khac
                td.HINH_DANG_THUA_DAT = this.txtTTTDatHinhDangKhac.Text.Trim();
            }

            if (this.rbMatTien.Checked)
                td.LOAI_DUONG_PHO = 14;             //lay tu bang tham so : mat tien
            else
                td.LOAI_DUONG_PHO = 15;             //lay tu bang tham so : hem

            if (!String.Empty.Equals(this.txtTTTDatMatTienLoGioi.Text.Trim()))
                td.LO_GIOI = Double.Parse(this.txtTTTDatMatTienLoGioi.Text.Trim());
            td.SO_MAT_TIEN = this.txtTTTDatMatTienSoMT.Text.Trim();

            //*********************************************************************************
            //IMPORTANT
            //LAY FIELD THUA_DAT.NOI_DUNG_LAN_DUONG LAM TEN NGUOI THU NHAP. 
            //*********************************************************************************
            td.NOI_DUNG_LAN_DUONG = this.txtTenNguoiThuThap.Text.Trim();


            if (this.rbTTTDatMatTienMotChieu.Checked)
                td.LOAI_LAN_DUONG = 16;             //lay tu bang tham so : mot chieu
            else if (this.rbTTTDatMatTienHaiChieu.Checked)
                td.LOAI_LAN_DUONG = 17;             //lay tu bang tham so : hai chieu
            else if (this.rbTTTDatMatTienPhanCach.Checked)
                td.LOAI_LAN_DUONG = 18;             //lay tu bang tham so : co dai phan cach
            if (rbHemCap1.Checked)
            {
                if (this.rbTTTDatDuongPhoCap1.Checked)
                    td.LOAI_HEM = 19;             //lay tu bang tham so : hem cap 1
                else if (this.rbTTTDatDuongPhoCap2.Checked)
                    td.LOAI_HEM = 20;             //lay tu bang tham so : hem cap 2
                else if (this.rbTTTDatDuongPhoCap3.Checked)
                    td.LOAI_HEM = 21;             //lay tu bang tham so : hem cap 3
                else if (this.rbTTTDatDuongPhoCap4.Checked)
                    td.LOAI_HEM = 22;             //lay tu bang tham so : hem cap 4
            }
            else
            {
                if (this.rbTTTHemCapKhacCap1.Checked)
                    td.LOAI_HEM = 23;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 1
                else if (this.rbTTTHemCapKhacCap2.Checked)
                    td.LOAI_HEM = 24;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 2
                else if (this.rbTTTHemCapKhacCap3.Checked)
                    td.LOAI_HEM = 25;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 3
                else if (this.rbTTTHemCapKhacCap4.Checked)
                    td.LOAI_HEM = 26;             //tu dinh nghia, k co trong bang tham so: hem cap khac cap 4
            }
            return td;
        }

        private CO_SO_HA_TANG buildCoSoHaTang(HO_SO_THAM_DINH hstd)
        {
            CO_SO_HA_TANG csht = new CO_SO_HA_TANG();
            csht.MA_HO_SO = hstd.MA_HO_SO;
            //1: Co, 2: Khong:
            //1: Nhieu, 2 Trung binh, 3 It
            //1: Nhieu, 2 Trung binh, 3 It, 4 Khong co
            csht.DONG_HO_DIEN = this.cbxTTHTDienDHDienCo.Checked ? 1 : 0;
            csht.CAP_TREO = this.cbxTTHTDienCapTreoCo.Checked ? 1 : 0;
            csht.CAP_NGAM = this.cbxTTHTDienCapNgamCo.Checked ? 1 : 0;
            csht.DONG_HO_NUOC = this.cbxTTHTDienDHDienCo.Checked ? 1 : 0;
            csht.AP_LUC_NUOC = this.cbxTTHTNuocALNuocManh.Checked ? 1 : 0;
            csht.CHAT_LUONG_NUOC = this.cbxTTHTNuocCLNuocSach.Checked ? 1 : 0;
            csht.TIENG_ON = this.cbxTTHTMTTiengOnCo.Checked ? 1 : 0;
            csht.KHOI_BUI = this.cbxTTHTMTKhoiBuiNhieu.Checked ? 1 : (this.cbxTTHTMTKhoiBuiTrungBinh.Checked ? 2 : 3);
            csht.TINH_TRANG_NGAP = this.cbxTTHTMTTTNgapNhieu.Checked ? 1 : (this.cbxTTHTMTTTNgapTrungBinh.Checked ? 2 : (this.cbxTTHTMTTTNgapIt.Checked ? 3 : 4));
            csht.HE_THONG_CHIEU_SANG = this.cbxTTHTANTTChieuSangCo.Checked ? 1 : 0;
            csht.CHOT_BAO_VE = this.cbxTTHTANTTDanPhongCo.Checked ? 1 : 0;
            csht.AN_NINH = this.cbxTTHTANTTTot.Checked ? 1 : (this.cbxTTHTANTTTrungBinh.Checked ? 2 : 3);
            csht.GAN_CO_SO_GIAO_DUC = this.cbxYKKhacCSGDCo.Checked ? 1 : 0;
            csht.GAN_CO_SO_Y_TE = this.cbxYKKhacCSYTeCo.Checked ? 1 : 0;
            csht.GAN_CHO_TRUNG_TAM = this.cbxYKKhacTTTMCo.Checked ? 1 : 0;
            csht.GAN_CO_SO_GIAI_TRI = this.cbxYKKhacCSGiaiTriCo.Checked ? 1 : 0;
            csht.GAN_DICH_VU_CONG_CONG = this.cbxYKKhacDVCCCo.Checked ? 1 : 0;
            csht.GAN_THONG_TIN_LIEN_LAC = this.cbxYKKhacTTLLCo.Checked ? 1 : 0;
            return csht;
        }

        private CONG_TRINH_XAY_DUNG buildCongTrinhXayDung(HO_SO_THAM_DINH hstd)
        {
            CONG_TRINH_XAY_DUNG ctxd = new CONG_TRINH_XAY_DUNG();
            ctxd.MA_HO_SO = hstd.MA_HO_SO;
            ctxd.NOI_DUNG_LOAI_NHA = "";
            if (this.rbTTCTLoaiNhaBietThu.Checked)
                ctxd.LOAI_NHA = 23;                 //lay tu bang THAM SO: biet thu
            else if (this.rbTTCTLoaiNhaPho.Checked)
                ctxd.LOAI_NHA = 24;                 //lay tu bang THAM SO: pho
            else if (this.rbTTCTLoaiNhaLienKe.Checked)
                ctxd.LOAI_NHA = 25;                 //lay tu bang THAM SO: lien ke
            else if (this.rbTTCTLoaiNhaChungCu.Checked)
                ctxd.LOAI_NHA = 26;                 //lay tu bang THAM SO: chung cu
            else if (this.rbTTCTLoaiNhaKhac.Checked)
            {
                ctxd.LOAI_NHA = 27;                 //lay tu bang THAM SO: khac
                ctxd.NOI_DUNG_LOAI_NHA = this.txtTTCTLoaiNhaChungChuKhac.Text.Trim();
            }

            if (this.rbTTCTCapNha1.Checked)
                ctxd.LOAI_CAP_NHA = 28;                 //lay tu bang THAM SO: cap 1
            else if (this.rbTTCTCapNha2.Checked)
                ctxd.LOAI_CAP_NHA = 29;                 //lay tu bang THAM SO: cap 2
            else if (this.rbTTCTCapNha3.Checked)
                ctxd.LOAI_CAP_NHA = 30;                 //lay tu bang THAM SO: cap 3
            else if (this.rbTTCTCapNha4.Checked)
                ctxd.LOAI_CAP_NHA = 31;                 //lay tu bang THAM SO: cap 4
            else if (this.rbTTCTCapNhaTam.Checked)
                ctxd.LOAI_CAP_NHA = 32;                 //lay tu bang THAM SO: khac                

            if (this.rbTTCTHuongNhaDong.Checked)
                ctxd.HUONG_NHA = 33;                 //lay tu bang THAM SO: D
            else if (this.rbTTCTHuongNhaDongBac.Checked)
                ctxd.HUONG_NHA = 34;                 //lay tu bang THAM SO: DB
            else if (this.rbTTCTHuongNhaTay.Checked)
                ctxd.HUONG_NHA = 35;                 //lay tu bang THAM SO: T
            else if (this.rbTTCTHuongNhaTayBac.Checked)
                ctxd.HUONG_NHA = 36;                 //lay tu bang THAM SO: TB
            if (this.rbTTCTHuongNhaNam.Checked)
                ctxd.HUONG_NHA = 37;                 //lay tu bang THAM SO: N
            else if (this.rbTTCTHuongNhaDongNam.Checked)
                ctxd.HUONG_NHA = 38;                 //lay tu bang THAM SO: DN
            else if (this.rbTTCTHuongNhaBac.Checked)
                ctxd.HUONG_NHA = 39;                 //lay tu bang THAM SO: B
            else if (this.rbTTCTHuongNhaTayNam.Checked)
                ctxd.HUONG_NHA = 40;                 //lay tu bang THAM SO: TN

            ctxd.GIAY_PHEP_XAY_DUNG = this.rbTTCTGiayPhepCo.Checked ? "Y" : "N";

            if (!String.Empty.Equals(this.txtTTCTDienTichCongNhan.Text.Trim()))
                ctxd.DIEN_TICH_XD_CONG_NHAN = Double.Parse(this.txtTTCTDienTichCongNhan.Text.Trim());
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Diện tích sàn công nhận' cần khảo sát !<br>";
                this.txtTTCTDienTichCongNhan.Style.Add("background-color", "red");
            }
            if (!String.Empty.Equals(this.txtTTCTDienTichCongNhan.Text.Trim()))
                ctxd.DIEN_TICH_XD_KHONG_CONG_NHAN = Double.Parse(this.txtTTCTDienTichCongNhan.Text.Trim());
            try
            {
                int year = Int32.Parse(this.txtTTCTNamXayDungXong.Text.Trim());
                ctxd.NAM_XAY_DUNG = new DateTime(year, 1, 1);
                if (year < 1000 || year > 2100)
                    ctxd.NAM_XAY_DUNG = DateTime.Now;
            }
            catch (Exception)
            {
                ctxd.NAM_XAY_DUNG = DateTime.Now;
            }

            if (!String.Empty.Equals(this.txtTTCTSoNamSuDung.Text.Trim()))
                ctxd.SO_NAM_SU_DUNG = Double.Parse(this.txtTTCTSoNamSuDung.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTDonGiaBinhQuan.Text.Trim()))
                ctxd.DON_GIA_XAY_DUNG = Double.Parse(this.txtTTCTDonGiaBinhQuan.Text.Trim());
            else
            {
                this.lblErrorMessge.Text += "Bạn chưa điền thông tin về 'Đơn giá xây dựng bình quân' cần khảo sát !<br>";
                this.txtTTCTDonGiaBinhQuan.Style.Add("background-color", "red");
            }

            if (!String.Empty.Equals(this.txtTTCTTuoiDoi.Text.Trim()))
                ctxd.TUOI_DOI_KINH_TE = Double.Parse(this.txtTTCTTuoiDoi.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTGiaTriXayDung.Text.Trim()))
                ctxd.GIA_TRI_CONG_TRINH = Double.Parse(this.txtTTCTGiaTriXayDung.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTSoLanSuaChua.Text.Trim()))
                ctxd.SO_LAN_SUA_CHUA = Int32.Parse(this.txtTTCTSoLanSuaChua.Text.Trim());

            return ctxd;
        }

        private VAT_LIEU_XAY_DUNG buildVatLieuXayDung(HO_SO_THAM_DINH hstd)
        {
            VAT_LIEU_XAY_DUNG vlxd = new VAT_LIEU_XAY_DUNG();
            vlxd.MA_HO_SO = hstd.MA_HO_SO;

            vlxd.NOI_DUNG_KET_CAU_KHUNG = "";
            if (this.rbTTCTVatLieuKhungBTCT.Checked)
                vlxd.LOAI_KET_CAU_KHUNG = 41;                 //lay tu bang THAM SO: BTCT
            else if (this.rbTTCTVatLieuKhungGach.Checked)
                vlxd.LOAI_KET_CAU_KHUNG = 42;                 //lay tu bang THAM SO: Gach
            else if (this.rbTTCTVatLieuKhungGo.Checked)
                vlxd.LOAI_KET_CAU_KHUNG = 43;                 //lay tu bang THAM SO: Go
            else if (this.rbTTCTVatLieuKhungKhac.Checked)
            {
                vlxd.LOAI_KET_CAU_KHUNG = 44;                 //lay tu bang THAM SO: khac
                vlxd.NOI_DUNG_KET_CAU_KHUNG = this.txtTTCTVatLieuKhungKhac.Text.Trim();
            }

            vlxd.NOI_DUNG_KET_CAU_MONG = "";
            if (this.rbTTCTVatLieuMongCoc.Checked)
                vlxd.LOAI_KET_CAU_MONG = 45;                 //lay tu bang THAM SO: Coc
            else if (this.rbTTCTVatLieuMongBang.Checked)
                vlxd.LOAI_KET_CAU_MONG = 46;                 //lay tu bang THAM SO: Bang
            else if (this.rbTTCTVatLieuMongBe.Checked)
                vlxd.LOAI_KET_CAU_MONG = 47;                 //lay tu bang THAM SO: Be
            else if (this.rbTTCTVatLieuMongKhac.Checked)
            {
                vlxd.LOAI_KET_CAU_MONG = 48;                 //lay tu bang THAM SO: khac
                vlxd.NOI_DUNG_KET_CAU_MONG = this.txtTTCTVatLieuMongKhac.Text.Trim();
            }

            vlxd.NOI_DUNG_KET_CAU_TUONG_NHA = "";
            if (this.rbTTCTVatLieuTuongGach.Checked)
                vlxd.LOAI_KET_CAU_TUONG_NHA = 49;                 //lay tu bang THAM SO: Gacg
            else if (this.rbTTCTVatLieuTuongGo.Checked)
                vlxd.LOAI_KET_CAU_TUONG_NHA = 50;                 //lay tu bang THAM SO: Go
            else if (this.rbTTCTVatLieuTuongTole.Checked)
                vlxd.LOAI_KET_CAU_TUONG_NHA = 51;                 //lay tu bang THAM SO: Tole
            else if (this.rbTTCTVatLieuTuongKhac.Checked)
            {
                vlxd.LOAI_KET_CAU_TUONG_NHA = 52;                 //lay tu bang THAM SO: khac
                vlxd.NOI_DUNG_KET_CAU_TUONG_NHA = this.txtTTCTVatLieuTuongKhac.Text.Trim();
            }

            vlxd.NOI_DUNG_KET_CAU_NEN = "";
            if (this.rbTTCTVatLieuNenGach.Checked)
                vlxd.LOAI_KET_CAU_NEN = 53;                 //lay tu bang THAM SO: Gacg
            else if (this.rbTTCTVatLieuNenGo.Checked)
                vlxd.LOAI_KET_CAU_NEN = 54;                 //lay tu bang THAM SO: Go
            else if (this.rbTTCTVatLieuNenXiMang.Checked)
                vlxd.LOAI_KET_CAU_NEN = 55;                 //lay tu bang THAM SO: Xi mang
            else if (this.rbTTCTVatLieuNenKhac.Checked)
            {
                vlxd.LOAI_KET_CAU_NEN = 56;                 //lay tu bang THAM SO: khac
                vlxd.NOI_DUNG_KET_CAU_NEN = this.txtrbTTCTVatLieuNenKhac.Text.Trim();
            }

            vlxd.NOI_DUNG_KET_CAU_MAI = "";
            if (this.rbTTCTVatLieuMaiBang.Checked)
                vlxd.LOAI_KET_CAU_MAI = 57;                 //lay tu bang THAM SO: Bang
            else if (this.rbTTCTVatLieuMaiNgoi.Checked)
                vlxd.LOAI_KET_CAU_MAI = 58;                 //lay tu bang THAM SO: Ngoi
            else if (this.rbTTCTVatLieuMaiTole.Checked)
                vlxd.LOAI_KET_CAU_MAI = 59;                 //lay tu bang THAM SO: Tole
            else if (this.rbTTCTVatLieuMaiKhac.Checked)
            {
                vlxd.LOAI_KET_CAU_MAI = 60;                 //lay tu bang THAM SO: khac
                vlxd.NOI_DUNG_KET_CAU_MAI = this.txtTTCTVatLieuMaiKhac.Text.Trim();
            }

            if (!String.Empty.Equals(this.txtTTCTVatLieuChatLuongMong.Text.Trim()))
                vlxd.CHAT_LUONG_MONG = Double.Parse(this.txtTTCTVatLieuChatLuongMong.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuChatLuongSan.Text.Trim()))
                vlxd.CHAT_LUONG_SAN = Double.Parse(this.txtTTCTVatLieuChatLuongSan.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuChatLuongTuong.Text.Trim()))
                vlxd.CHAT_LUONG_TUONG = Double.Parse(this.txtTTCTVatLieuChatLuongTuong.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuChatLuongTran.Text.Trim()))
                vlxd.CHAT_LUONG_TRAN = Double.Parse(this.txtTTCTVatLieuChatLuongTran.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuChatLuongMai.Text.Trim()))
                vlxd.CHAT_LUONG_MAI = Double.Parse(this.txtTTCTVatLieuChatLuongMai.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuTiLeKetCauMong.Text.Trim()))
                vlxd.TI_LE_KET_CAU_MONG = Double.Parse(this.txtTTCTVatLieuTiLeKetCauMong.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuTiLeKetCauSan.Text.Trim()))
                vlxd.TI_LE_KET_CAU_SAN = Double.Parse(this.txtTTCTVatLieuTiLeKetCauSan.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuTiLeKetCauTuong.Text.Trim()))
                vlxd.TI_LE_KET_CAU_TUONG = Double.Parse(this.txtTTCTVatLieuTiLeKetCauTuong.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuTiLeKetCauTran.Text.Trim()))
                vlxd.TI_LE_KET_CAU_TRAN = Double.Parse(this.txtTTCTVatLieuTiLeKetCauTran.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuTiLeKetCauMai.Text.Trim()))
                vlxd.TI_LE_KET_CAU_MAI = Double.Parse(this.txtTTCTVatLieuTiLeKetCauMai.Text.Trim());

            if (!String.Empty.Equals(this.txtTTCTVatLieuSoPhongNgu.Text.Trim()))
                vlxd.SO_PHONG_NGU = Int32.Parse(this.txtTTCTVatLieuSoPhongNgu.Text.Trim());
            if (!String.Empty.Equals(this.txtTTCTVatLieuSoNhaVS.Text.Trim()))
                vlxd.SO_NHA_VE_SINH = Int32.Parse(this.txtTTCTVatLieuSoNhaVS.Text.Trim());

            vlxd.NOI_DUNG_TRANG_TRI_NOI_THAT = "";// --> non-set
            if (this.rbTTCTVatLieuTTNTCaoCap.Checked)
                vlxd.LOAI_TRANG_TRI_NOI_THAT = 61;                 //lay tu bang THAM SO: cao cap
            else if (this.rbTTCTVatLieuTTNTKha.Checked)
                vlxd.LOAI_TRANG_TRI_NOI_THAT = 62;                 //lay tu bang THAM SO: kha
            else if (this.rbTTCTVatLieuTTNTTrungBinh.Checked)
                vlxd.LOAI_TRANG_TRI_NOI_THAT = 63;                 //lay tu bang THAM SO: trung binh
            else if (this.rbTTCTVatLieuTTNTTam.Checked)
            {
                vlxd.LOAI_TRANG_TRI_NOI_THAT = 64;                 //lay tu bang THAM SO: tam                
            }

            //Cheat code, vi bo tu single choice sang multi-choice
            //Dung NOI_DUNG_CONG_TRINH_XAY_DUNG de chua du lieu
            vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG = "";
            vlxd.LOAI_CONG_TRINH_XAY_DUNG = 68;
            if (this.rbTTCTVatLieuCongTrinhSanThuong.Checked)
                vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG += "65";                 //lay tu bang THAM SO: san thuong
            if (this.rbTTCTVatLieuCongTrinhHoBoi.Checked)
                vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG += "66";                 //lay tu bang THAM SO: ho boi
            if (this.rbTTCTVatLieuCongTrinhSan.Checked)
                vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG += "67";                 //lay tu bang THAM SO: san
            if (this.rbTTCTVatLieuCongTrinhKhac.Checked)
            {
                vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG += "68";
                //lay tu bang THAM SO: khac
                vlxd.NOI_DUNG_CONG_TRINH_XAY_DUNG += "<khac>" + txtTTCTVatLieuCongTrinhKhac.Text.Trim() + "<khac>";
            }
            return vlxd;
        }

        #endregion

        #region Disable mode
        private void _disableAllControl(ControlCollection colectionControl)
        {
            try
            {
                foreach (Control item in colectionControl)
                {
                    if (item is TextBox)
                    {
                        (item as TextBox).Enabled = false;
                    }
                    if (item is DropDownList)
                    {
                        (item as DropDownList).Enabled = false;
                    }
                    if (item is RadioButton)
                    {
                        (item as RadioButton).Attributes.Add("onclick", "return false;");
                    }
                    if (item is CheckBox)
                    {
                        (item as CheckBox).Attributes.Add("onclick", "return false;");
                    }
                    if (item is CalendarExtender)
                    {
                        (item as CalendarExtender).Enabled = false;
                    }
                    this._disableAllControl(item.Controls);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region upload file
        protected string _upload_document()
        {
            string absolutepath = Server.MapPath(ConfigurationManager.AppSettings["PathUploadDocument"]);
            String finalFilePath = "";
            if (Request.Files.Count > 0)
            {
                HttpPostedFile file = Request.Files[0];

                if (!String.IsNullOrEmpty(file.FileName))
                {
                    try
                    {
                        // upload new picture
                        string filepath = absolutepath + "\\" + file.FileName;
                        file.SaveAs(filepath);

                        FileInfo fi = new FileInfo(filepath);

                        string newfilename = Guid.NewGuid().ToString();
                        finalFilePath = absolutepath + "\\" + newfilename + "." + fi.Extension;
                        File.Copy(filepath, finalFilePath);

                        // write new data to database
                        //CPicture pic = new CPicture
                        //{
                        //    PictureName = file.FileName,
                        //    PictureText = Server.HtmlEncode(txtPictureText.Text),
                        //};
                        //PictureManager picture_access = new PictureManager(Server.MapPath(data_file_path));
                        //if (picture_access.insert_new_picture(pic))
                        //{
                        //    // if upload successfully
                        //    DisplayData();
                        //}
                        //else
                        //{
                        //    lblResult.Text = "Upload failed";
                        //    // delete uploaded file
                        //    FileInfo f = new FileInfo(filepath);
                        //    if (f.Exists)
                        //    {
                        //        f.Delete();
                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        return "";
                    }
                }
            }
            return finalFilePath;
        }
        #endregion
    }
}
